package org.jeecg.modules.persongohome.service.impl;

import org.jeecg.modules.persongohome.entity.PersonGoHome;
import org.jeecg.modules.persongohome.entity.TemperatureCheck;
import org.jeecg.modules.persongohome.entity.PersonGohomeContact;
import org.jeecg.modules.persongohome.entity.PersonGuardianship;
import org.jeecg.modules.persongohome.mapper.TemperatureCheckMapper;
import org.jeecg.modules.persongohome.mapper.PersonGohomeContactMapper;
import org.jeecg.modules.persongohome.mapper.PersonGuardianshipMapper;
import org.jeecg.modules.persongohome.mapper.PersonGoHomeMapper;
import org.jeecg.modules.persongohome.service.IPersonGoHomeService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 返乡人员
 * @Author: jeecg-boot
 * @Date:   2020-01-30
 * @Version: V1.0
 */
@Service
public class PersonGoHomeServiceImpl extends ServiceImpl<PersonGoHomeMapper, PersonGoHome> implements IPersonGoHomeService {

	@Autowired
	private PersonGoHomeMapper personGoHomeMapper;
	@Autowired
	private TemperatureCheckMapper temperatureCheckMapper;
	@Autowired
	private PersonGohomeContactMapper personGohomeContactMapper;
	@Autowired
	private PersonGuardianshipMapper personGuardianshipMapper;
	
	@Override
	@Transactional
	public void saveMain(PersonGoHome personGoHome, List<TemperatureCheck> temperatureCheckList,List<PersonGohomeContact> personGohomeContactList,List<PersonGuardianship> personGuardianshipList) {
		personGoHomeMapper.insert(personGoHome);
		if(temperatureCheckList!=null && temperatureCheckList.size()>0) {
			for(TemperatureCheck entity:temperatureCheckList) {
				//外键设置
				entity.setPersonId(personGoHome.getId());
				temperatureCheckMapper.insert(entity);
			}
		}
		if(personGohomeContactList!=null && personGohomeContactList.size()>0) {
			for(PersonGohomeContact entity:personGohomeContactList) {
				//外键设置
				entity.setPersonId(personGoHome.getId());
				personGohomeContactMapper.insert(entity);
			}
		}
		if(personGuardianshipList!=null && personGuardianshipList.size()>0) {
			for(PersonGuardianship entity:personGuardianshipList) {
				//外键设置
				entity.setPersonId(personGoHome.getId());
				personGuardianshipMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(PersonGoHome personGoHome,List<TemperatureCheck> temperatureCheckList,List<PersonGohomeContact> personGohomeContactList,List<PersonGuardianship> personGuardianshipList) {
		personGoHomeMapper.updateById(personGoHome);
		
		//1.先删除子表数据
		temperatureCheckMapper.deleteByMainId(personGoHome.getId());
		personGohomeContactMapper.deleteByMainId(personGoHome.getId());
		personGuardianshipMapper.deleteByMainId(personGoHome.getId());
		
		//2.子表数据重新插入
		if(temperatureCheckList!=null && temperatureCheckList.size()>0) {
			for(TemperatureCheck entity:temperatureCheckList) {
				//外键设置
				entity.setPersonId(personGoHome.getId());
				temperatureCheckMapper.insert(entity);
			}
		}
		if(personGohomeContactList!=null && personGohomeContactList.size()>0) {
			for(PersonGohomeContact entity:personGohomeContactList) {
				//外键设置
				entity.setPersonId(personGoHome.getId());
				personGohomeContactMapper.insert(entity);
			}
		}
		if(personGuardianshipList!=null && personGuardianshipList.size()>0) {
			for(PersonGuardianship entity:personGuardianshipList) {
				//外键设置
				entity.setPersonId(personGoHome.getId());
				personGuardianshipMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		temperatureCheckMapper.deleteByMainId(id);
		personGohomeContactMapper.deleteByMainId(id);
		personGuardianshipMapper.deleteByMainId(id);
		personGoHomeMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			temperatureCheckMapper.deleteByMainId(id.toString());
			personGohomeContactMapper.deleteByMainId(id.toString());
			personGuardianshipMapper.deleteByMainId(id.toString());
			personGoHomeMapper.deleteById(id);
		}
	}
	
}
